unit uMainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Generics.collections,  uNiuEventBus,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Edit1: TEdit;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    procedure Button7Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
    procedure Log(const aMsg:String; aParms:array of const);
  public

    { Public declarations }
    [Channel('MemoChange', TThreadMode.tmMainThread)]
    procedure OnMemoChangeEvent(const aParams:IParameters);

    [Channel('ResponseMemoChangeEvent', TThreadMode.tmMainThread)]
    procedure OnResponseMemoChangeEvent(const aParams:IParameters);
  end;

var
  Form1: TForm1;

implementation

uses uNiuPlugin, Commons;


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
   with GlobalEventBus do begin
     RegisterSubscriber(Self);
   end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // if Assigned(dll_DestroyDataModule) then {dll_DestroyDataModule()};

   with GlobalEventBus do begin
     unRegisterSubscriber(Self);
   end;
end;

procedure TForm1.Log(const aMsg: String; aParms: array of const);
begin
  var s := format(aMsg, aParms);
  Memo1.Lines.Add(s);
end;


procedure TForm1.Button7Click(Sender: TObject);
begin
  with ComboBox1 do begin
     var mTxt:AnsiString := ComboBox1.Text+'_dll';
//     dll_RegisterService(PAnsiChar(mTxt),nil);
  end;
end;


procedure TForm1.Button8Click(Sender: TObject);
var
  mService:IDemoService;
begin
  with NiuServiceManager do begin
    if not TryGetService(IDemoService, '1', mService) then Exit;

    Memo1.Lines.Add(Format('IDemoServie: %d + %d = %d',[2, 3, mService.add(2, 3)]));
  end;
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
  with NiuServiceManager do begin
    for var mIntf in TryGetServices(IDemoService) do begin
       var mService := mIntf as IDemoService;
       Memo1.Lines.Add(Format('IDemoServie: %d + %d = %d',[2, 3, mService.add(2, 3)]));
    end;
  end;
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
  for var I := 0 to 99 do Button9Click(Sender);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  with ComboBox1 do begin
     var mTxt:AnsiString := ComboBox1.Text+'_dll';
//     dll_unRegisterService(PAnsiChar(mTxt));
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  mObject:Pointer;
begin
  with  ComboBox1 do begin
     var mTxt:AnsiString := ComboBox1.Text+'_dll';
//     var mResult := dll_TryGetService(PAnsiChar(mTxt), mObject);
//     Log('dll_TryGetService: %s %d', [mTxt, Integer(mResult)]);
  end;

end;



procedure TForm1.Edit1Change(Sender: TObject);
begin
//  LEvent := GetMemoEvent;
//  LEvent.Text := Edit1.Text;
//  GlobalEventBus.Post(LEvent);
  GlobalEventBus.Post('MemoChange', NewParams([Edit1.Text]));
end;


procedure TForm1.OnMemoChangeEvent(const aParams: IParameters);
begin
  var mCount := aParams.Count;
  if mCount<>1 then exit;
  var mParam1 := aParams[0].asString;
  Log('MAIN: OnMemoChangeEvent: %s',[mParam1]);
end;

procedure TForm1.OnResponseMemoChangeEvent(const aParams: IParameters);
begin
  var mCount := aParams.Count;
  if mCount<2 then exit;

  var mParam1 := aParams[0].asString;
  var mParam2 := aParams[1].asString;

  var mStamp := FormatDateTime('c', Now());
  Log('%s %s %s',[mStamp, mParam1, mParam2]);

end;

initialization

finalization

end.
